/*
 * PySL:  Use S-Lang from Python (and vice versa)
 * Copyright (C) 2005,2006 Christopher J. Stawarz <chris@pseudogreen.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */



#include "libpysl.h"



/******************************************************************************
 *
 * Public module functions
 *
 *****************************************************************************/



PyDoc_STRVAR(pysl_load_string__doc__,
"Evaluate a string of S-Lang code, optionally in a specified\n\
namespace.");

static PyObject *
pysl_load_string(PyObject *self, PyObject *args)
{
  char *str;
  char *ns_name = NULL;

  if (!PyArg_ParseTuple(args, "s|z", &str, &ns_name))
    return NULL;

  if (-1 == SLns_load_string(str, ns_name)) {
    libpysl_slang_to_python_error();
    return NULL;
  }

  Py_RETURN_NONE;
}


PyDoc_STRVAR(pysl_is_defined__doc__,
"Test whether a name is known to the S-Lang interpreter as a variable\n\
or function, optionally in a specified namespace.  The function\n\
returns one of the following values:\n\
\n\
  +1 for an intrinsic function\n\
  +2 for a user defined function\n\
  -1 for an intrinsic variable\n\
  -2 for a user defined variable\n\
   0 if the name is undefined\n\
");

static PyObject *
pysl_is_defined(PyObject *self, PyObject *args)
{
  PyObject *rtn_val = NULL;
  char *name = NULL;
  char *ns_name = NULL;
  char *ns_qual_name = NULL;

  if (!PyArg_ParseTuple(args, "s|z", &name, &ns_name))
    goto error;

  if (NULL == (ns_qual_name = libpysl_ns_qualify_name(name, ns_name)))
    goto error;

  rtn_val = Py_BuildValue("i", SLang_is_defined(ns_qual_name));

 error:
  free(ns_qual_name);

  return rtn_val;
}


PyDoc_STRVAR(pysl_push__doc__,
"Push an object on to the S-Lang stack, converting or wrapping it\n\
as necessary.\n");

static PyObject *
pysl_push(PyObject *self, PyObject *args)
{
  PyObject *obj = NULL;

  if (!PyArg_ParseTuple(args, "O", &obj) ||
      -1 == libpysl_push_pyobject(obj)) {
    libpysl_slang_to_python_error();
    return NULL;
  }

  Py_RETURN_NONE;
}


PyDoc_STRVAR(pysl_pop__doc__,
"Pop an object off of the S-Lang stack, converting or wrapping it\n\
as necessary.\n");

static PyObject *
pysl_pop(PyObject *self, PyObject *args)
{
  PyObject *obj = NULL;

  if (SLstack_depth() < 1) {
    PyErr_SetString(PySL_SLangError, "S-Lang stack is empty");
    return NULL;
  }

  if (NULL == (obj = libpysl_pop_pyobject())) {
    libpysl_slang_to_python_error();
    return NULL;
  }

  return obj;
}



/******************************************************************************
 *
 * Module initialization
 *
 *****************************************************************************/



static PyMethodDef PySL_Methods[] = {
  {"load_string",  pysl_load_string, METH_VARARGS, pysl_load_string__doc__},
  {"is_defined",  pysl_is_defined, METH_VARARGS, pysl_is_defined__doc__},
  {"push",  pysl_push, METH_VARARGS, pysl_push__doc__},
  {"pop",  pysl_pop, METH_VARARGS, pysl_pop__doc__},
  {NULL, NULL, 0, NULL}	 /* Sentinel */
};


PyMODINIT_FUNC
init_pysl(void)
{
  PyObject *m;	/* Module object */

  /*
   * Initialize libpysl
   */

  if (-1 == libpysl_init(1)) {
    libpysl_slang_to_python_error();
    return;
  }

  /*
   * Create the module object
   */

  if (NULL == (m = Py_InitModule("_pysl", PySL_Methods)) ||
      -1 == PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION))
    return;

  /*
   * Add the SLangObject type and SLangError exception to the module
   */

  Py_INCREF(&PySL_SLangObject_Type);
  if (-1 == PyModule_AddObject(m, "SLangObject",
			       (PyObject*)&PySL_SLangObject_Type))
    return;

  Py_INCREF(PySL_SLangError);
  if (-1 == (PyModule_AddObject(m, "SLangError", PySL_SLangError)))
    return;


  /*
   * Add the error-map dictionaries to the module
   */

  Py_INCREF(LibPySL_Py2SL_Error_Map);
  if (-1 == (PyModule_AddObject(m, "py2sl_errormap", LibPySL_Py2SL_Error_Map)))
    return;

  Py_INCREF(LibPySL_SL2Py_Error_Map);
  if (-1 == (PyModule_AddObject(m, "sl2py_errormap", LibPySL_SL2Py_Error_Map)))
    return;
}
